//cleaning ipeds outcomes (enrollment and completion) data here

//globals
global direc "[YOUR PATH HERE]"
global raw_data "${direc}/data/raw"
global code "${direc}/code"
global data "${direc}/data"


clear all

//switches 
local switch_completions = 1
local switch_enrollment  = 1

if `switch_completions'{
	//begin with completions
	use "${data}/ipeds/build_ipeds_completion.dta", clear
	
	replace year = 1990 if year == 8990
	replace year = 1995 if year == 9495
	replace year = 1996 if year == 9596
	replace year = 1997 if year == 9697
	replace year = 1998 if year == 9798
	replace year = 1999 if year == 9899


	//harmonizing race variables across years
	foreach yr in 2010 2009 2008 2007 2006 2005 2004 2003 2002 1999{
		replace ctotalt = crace24 if missing(ctotalt) & year == `yr'
		replace ctotalm = crace15 if missing(ctotalm) & year == `yr'
		replace ctotalw = crace16 if missing(ctotalw) & year == `yr'
		replace caiant = crace19 if missing(caiant) & year == `yr'
		replace caianm = crace05 if missing(caianm) & year == `yr'
		replace caianw = crace06 if missing(caianw) & year == `yr'
		replace casiat = crace20 if missing(casiat) & year == `yr'
		replace casiam = crace07 if missing(casiam) & year == `yr'
		replace casiaw = crace08 if missing(casiaw) & year == `yr'
		replace cbkaat = crace18 if missing(cbkaat) & year == `yr'
		replace cbkaam = crace03 if missing(cbkaam) & year == `yr'
		replace cbkaaw = crace04 if missing(cbkaaw) & year == `yr'
		replace chispt = crace21 if missing(chispt) & year == `yr'
		replace chispm = crace09 if missing(chispm) & year == `yr'
		replace chispw = crace10 if missing(chispw) & year == `yr'
		replace cwhitt = crace22 if missing(cwhitt) & year == `yr'
		replace cwhitm = crace11 if missing(cwhitm) & year == `yr'
		replace cwhitw = crace12 if missing(cwhitw) & year == `yr'
		replace cunknt = crace23 if missing(cunknt) & year == `yr'
		replace cunknm = crace13 if missing(cunknm) & year == `yr'
		replace cunknw = crace14 if missing(cunknw) & year == `yr'
		replace cnralt = crace17 if missing(cnralt) & year == `yr'
		replace cnralm = crace01 if missing(cnralm) & year == `yr'
		replace cnralw = crace02 if missing(cnralw) & year == `yr'	
	}
	foreach yr in 2001 2000 1998 1997 1996 1995 {
		replace ctotalm = crace15 if missing(ctotalm) & year == `yr'
		replace ctotalw = crace16 if missing(ctotalw) & year == `yr'
		replace caianm = crace05 if missing(caianm) & year == `yr'
		replace caianw = crace06 if missing(caianw) & year == `yr'
		replace cbkaam = crace03 if missing(cbkaam) & year == `yr'
		replace cbkaaw = crace04 if missing(cbkaaw) & year == `yr'
		replace chispm = crace09 if missing(chispm) & year == `yr'
		replace chispw = crace10 if missing(chispw) & year == `yr'
		replace cwhitm = crace11 if missing(cwhitm) & year == `yr'
		replace cwhitw = crace12 if missing(cwhitw) & year == `yr'
		replace cunknm = crace13 if missing(cunknm) & year == `yr'
		replace cunknw = crace14 if missing(cunknw) & year == `yr'
		replace cnralm = crace01 if missing(cnralm) & year == `yr'
		replace cnralw = crace02 if missing(cnralw) & year == `yr'
		replace casiam = crace07 if missing(casiam) & year == `yr'
		replace casiaw = crace08 if missing(casiaw) & year == `yr'
	}
	foreach yr in 1994 1993 1992 1991 1990 1989 1988 1987 1986 1985 1984{
		replace ctotalm = crace15 if missing(ctotalm) & year == `yr'
		replace ctotalw = crace16 if missing(ctotalw) & year == `yr'
	}
	foreach cat in total aian asia bkaa hisp whit unkn nral{
		replace c`cat'm = 0 if missing(c`cat'm)
		replace c`cat'w = 0 if missing(c`cat'w)
		replace c`cat't = c`cat'w + c`cat'm if missing(c`cat't)
	}

	save "${data}/ipeds/clean_ipeds_completion.dta", replace
}

if `switch_enrollment'{
	//move to enrollments
	use "${data}/ipeds/build_ipeds_enrollment.dta", clear

	//harmonizing race variables across years
	foreach yr in 2009 2008 2007 2006 2005 2004 2003 2002 1999{
		replace eftotlt  = efrace24 if missing(eftotlt) & year == `yr'
		replace eftotlm  = efrace15 if missing(eftotlm) & year == `yr'
		replace eftotlw  = efrace16 if missing(eftotlw) & year == `yr'
		replace efaiant  = efrace19 if missing(efaiant)  & year == `yr'
		replace efaianm  = efrace05 if missing(efaianm)  & year == `yr'
		replace efaianw  = efrace06 if missing(efaianw)  & year == `yr'
		replace efbkaat  = efrace18 if missing(efbkaat)  & year == `yr'
		replace efbkaam  = efrace03 if missing(efbkaam)  & year == `yr'
		replace efbkaaw  = efrace04 if missing(efbkaaw)  & year == `yr'
		replace efhispt  = efrace21 if missing(efhispt)  & year == `yr'
		replace efhispm  = efrace09 if missing(efhispm)  & year == `yr'
		replace efhispw  = efrace10 if missing(efhispw)  & year == `yr'
		replace efwhitt  = efrace22 if missing(efwhitt)  & year == `yr'
		replace efwhitm  = efrace11 if missing(efwhitm)  & year == `yr'
		replace efwhitw  = efrace12 if missing(efwhitw)  & year == `yr'
		replace efunknt  = efrace23 if missing(efunknt)  & year == `yr'
		replace efunknm  = efrace13 if missing(efunknm)  & year == `yr'
		replace efunknw  = efrace14 if missing(efunknw)  & year == `yr'
		replace efnralt  = efrace17 if missing(efnralt)  & year == `yr'
		replace efnralm  = efrace01 if missing(efnralm)  & year == `yr'
		replace efnralw  = efrace02 if missing(efnralw)  & year == `yr'
		replace efasiat  = efrace20 if missing(efasiat) & year == `yr'
		replace efasiam  = efrace07 if missing(efasiam) & year == `yr'
		replace efasiaw  = efrace08 if missing(efasiaw) & year == `yr'
	}
	foreach yr in 2001 2000 1998 1997 1996 1995 1994 1993 1992 1991 1990 1988 1986 1984{
		replace eftotlm = efrace15 if missing(eftotlm) & year == `yr'
		replace eftotlw = efrace16 if missing(eftotlw) & year == `yr'
		replace efaianm  = efrace05 if missing(efaianm)  & year == `yr'
		replace efaianw  = efrace06 if missing(efaianw)  & year == `yr'
		replace efbkaam  = efrace03 if missing(efbkaam)  & year == `yr'
		replace efbkaaw  = efrace04 if missing(efbkaaw)  & year == `yr'
		replace efhispm  = efrace09 if missing(efhispm)  & year == `yr'
		replace efhispw  = efrace10 if missing(efhispw)  & year == `yr'
		replace efwhitm  = efrace11 if missing(efwhitm)  & year == `yr'
		replace efwhitw  = efrace12 if missing(efwhitw)  & year == `yr'
		replace efunknm  = efrace13 if missing(efunknm)  & year == `yr'
		replace efunknw  = efrace14 if missing(efunknw)  & year == `yr'
		replace efnralm  = efrace01 if missing(efnralm)  & year == `yr'
		replace efnralw  = efrace02 if missing(efnralw)  & year == `yr'	
		replace efasiam  = efrace07 if missing(efasiam) & year == `yr'
		replace efasiaw  = efrace08 if missing(efasiaw) & year == `yr'
	}
	foreach yr in 1989 1987 1985 1980{
		replace eftotlm = efrace15 if missing(eftotlm) & year == `yr'
		replace eftotlw = efrace16 if missing(eftotlw) & year == `yr'
	}
	foreach cat in totl aian asia bkaa hisp whit unkn nral{
		replace ef`cat'm = 0 if missing(ef`cat'm)
		replace ef`cat'w = 0 if missing(ef`cat'w)
		replace ef`cat't = ef`cat'w + ef`cat'm if missing(ef`cat't)
	}

	//cleaning part-time/full-time indicators
	replace efalevel = 22 if line == 8  & missing(efalevel)
	replace efalevel = 42 if line == 22 & missing(efalevel)


	save "${data}/ipeds/clean_ipeds_enrollment.dta", replace
}

